﻿using System;
using System.Data;
using Model.Entity;

namespace BLL
{
    public class BorrowBLL : BaseBLL
    {
        public DataTable getUser()
        {
            return dal.getUserDAL().getT();
        }

        public DataTable getUser(int userId)
        {
            return dal.getUserDAL().getT(userId);
        }        

        public Rfid canBorrow(String rfid)
        {
            return dal.getRfidDAL().canBorrow(rfid);
        }

        //我自己都觉得写得有点过分XD
        public DataTable borrowBook(int userId, String rfid)
        {
            User user = dal.getUserDAL().get(userId);
            if (dal.getBorrowDAL().getCountByUser(user.id) >= user.userType.enableBorrowCount)
            {
                return null;
            }
            int borrowId = dal.getBorrowDAL().save(new Borrow()
            {
                user = user,
                rfid = new Rfid() { rfid = rfid },
                borrowDate = DateTime.Now,
                shouldReturnDate = DateTime.Now.AddDays(user.userType.enableBorrowDay),
            });
            return borrowId != 0 ? dal.getRfidDAL().getT(borrowId) : null;
        }
    }
}
